package top.shiyiri.threading2;

/**
 * 使用同步方法来处理继承 Thread 类的线程安全问题
 * @author Aunean
 * @date 2021/12/25
 */
class Window2 extends Thread {
    private static int ticket = 100;

    @Override
    public void run() {
        while (ticket > 0) {
            show();
        }
    }

    //private synchronized void show() { //错误的，t1，t2，t3
    private static synchronized void show() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + ": 卖票，票号为：" + ticket);
            ticket --;
        }
    }
}


public class WindowTest2 {
    public static void main(String[] args) {

        Window2 t1 = new Window2();
        Window2 t2 = new Window2();
        Window2 t3 = new Window2();

        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");

        t1.start();
        t2.start();
        t3.start();
    }
}
